//
//  EATabooFoodViewController.m
//  WhatToEat
//
//  Created by 方力 on 15/10/29.
//  Copyright © 2015年 Mac. All rights reserved.
//

#import "EATabooFoodViewController.h"
#import "EATaboolFoodCell.h"
#import "EATabooReusableView.h"
@interface EATabooFoodViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate>
@property(strong,nonatomic)    UICollectionView * collectView;
@property(strong,nonatomic)NSMutableArray * alltabooFoods;
@property(strong,nonatomic)UITextField * tf;
@property(strong,nonatomic)UIButton * bt;
@end

@implementation EATabooFoodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self updateUI];
    
    
}

-(void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];
    

    
    
}

-(void)viewWillDisappear:(BOOL)animated
{

    [super viewWillDisappear:animated];


}

-(void)updateUI
{
    
    
    self.view.backgroundColor=kWhiteColor;
    
    ApplicationDelegate.tabooFoods;
    
    self.alltabooFoods=[@[@"小龙虾",@"海鲜",@"辣椒",@"海鱼",@"带鱼",@"羊肉",@"螃蟹",@"牛蛙",@"鸡蛋",@"猪手",@"牛耳",@"狗肉"] mutableCopy];
    
    
    
    
    
    
    
    
    
    


    self.navigationItem.title=@"禁忌食材";

    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ok"] style:0 target:self action:@selector(okAction)];
    
    UICollectionViewFlowLayout * layout=[[UICollectionViewFlowLayout alloc] init];

    layout.sectionInset=UIEdgeInsetsMake(60, 20, 44, 20);
    layout.itemSize=CGSizeMake(95, 43);
    layout.minimumLineSpacing=10;

    layout.footerReferenceSize=CGSizeMake(kUI_SCREEN_WIDTH, 144);
    
    
    self.collectView=[[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectView.height=kUI_SCREEN_HEIGHT-44;
    [self.collectView registerNib:[UINib nibWithNibName:@"EATaboolFoodCell" bundle:nil] forCellWithReuseIdentifier:@"EATaboolFoodCell"];
    [self.collectView registerNib:[UINib nibWithNibName:@"EATabooReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"EATabooReusableView"];
    self.collectView.delegate=self;
    self.collectView.dataSource=self;
    [self.view addSubview:self.collectView];
    self.collectView.backgroundColor=kWhiteColor;

    
    UILabel * l=[[UILabel alloc] initWithFrame:CGRectMake(32, 15, 100, 25)];
    
    
    l.text=@"已选食物:";
    
    [self.collectView addSubview:l];

    
    UITapGestureRecognizer * tp=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelAllKety)];
    
    
    [self.collectView addGestureRecognizer:tp];
}


-(void)setAlltabooFoods:(NSMutableArray *)alltabooFoods
{
    _alltabooFoods=alltabooFoods;

    self.tabooFoods=[@[ ApplicationDelegate.tabooFoods,[self getSubArrayFromAllArray:self.alltabooFoods toArray:ApplicationDelegate.tabooFoods]] mutableCopy];
    [self.collectView reloadData];

}


-(NSMutableArray *)getSubArrayFromAllArray:(NSMutableArray * )allArray toArray:(NSMutableArray *)toArray
{

    NSMutableArray * muArray=[NSMutableArray array];
    
    
    for (id id1 in allArray) {
        
        if ([toArray containsObject:id1]) {
            
            
            
            
        }else{
        
            [muArray addObject:id1];
        
        }
        
        
        
    }
    
    
    
    



    return muArray;

}




-(void)cancelAllKety
{
    [self.tf resignFirstResponder];
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{

    if (section==0) {
        return CGSizeMake(kUI_SCREEN_WIDTH, 144);
    }
    
    
    
    
    return CGSizeMake(0, 0);
    
    

}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    
    

    return CGSizeMake(95, 43);

}
-(void)okAction
{

    NSArray * arr=self.tabooFoods[0];
    


    NSString * arrString=[arr JSONString];
    

    
    [WebAPI webAPISignAddMd5tWithNote:@"user/profile" parm:@{
                                                             @"userID":ApplicationDelegate.userId,
                                                             @"taboo":arrString
                                                             
                                                             } completed:^(NSInteger state, AFHTTPRequestOperation *operation, id responseObject) {
       
                                                                 [EASaveUserMessage saveUserMessage:responseObject andView:nil];
            [self.navigationController popViewControllerAnimated:YES];
    }];
    
    
    
    
    
    
    



}

//- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//
//    UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
//    
//    attributes.size = CGSizeMake(80,30);
//
//    
//    return attributes;
//}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{

    return self.tabooFoods.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    NSMutableArray * arr=self.tabooFoods[section];
    
    
    
    
    
    return arr.count;

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    EATaboolFoodCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"EATaboolFoodCell" forIndexPath:indexPath];
    
        NSMutableArray * arr=self.tabooFoods[indexPath.section];
    
    
    cell.foodNameLabel.text=arr[indexPath.row];
    
    cell.tag=indexPath.row;
    
    
    
    if (indexPath.section==1) {
        
        
        cell.imageV.hidden=YES;
        
    }else
    {
        cell.imageV.hidden=NO;
    
    }
    
    
    
    
    cell.tapAction=^(UITapGestureRecognizer * tap){
    
        CGPoint p =[tap locationInView:tap.view];
        
        if (indexPath.section==0) {
            if (CGRectContainsPoint(CGRectMake(60,0, 30, 30), p) ) {
                
                NSMutableArray * arr0=[self.tabooFoods[0] mutableCopy];
                NSMutableArray * arr1=[self.tabooFoods[1] mutableCopy];
                NSString * food=arr0[indexPath.row];
                
                [arr0 removeObject:food];
                [arr1 addObject:food];
                
                self.tabooFoods=[@[arr0,arr1] mutableCopy];
                
                
                
                
               // [self.tabooFoods removeObjectAtIndex:indexPath.row];
                [UIView animateWithDuration:1 animations:^{
                     [self.collectView reloadData];
                }];
                
               
                
            }
 
        }else
        {
            NSMutableArray * arr0=[self.tabooFoods[0] mutableCopy];
            NSMutableArray * arr1=[self.tabooFoods[1] mutableCopy];
            NSString * food=arr1[indexPath.row];
            
            [arr0 addObject:food];
            [arr1 removeObject:food];
            
            self.tabooFoods=[@[arr0,arr1] mutableCopy];
            
            
            
            
            // [self.tabooFoods removeObjectAtIndex:indexPath.row];
            [UIView animateWithDuration:1 animations:^{
                [self.collectView reloadData];
            }];
        
        
        }

        
        
        NSLog(@"%@",tap);
        
    
    };

    return cell;

}


-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{

        
    
    UICollectionReusableView *headView;
    
 if([kind isEqual:UICollectionElementKindSectionFooter])
    {
        
        headView=nil;
        headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"EATabooReusableView" forIndexPath:indexPath];
    
        headView.backgroundColor=kWhiteColor;
        
        
        UIView * lineView=[[UIView alloc] initWithFrame:CGRectMake(16, 2, kUI_SCREEN_WIDTH-32, 1)];
        
        lineView.backgroundColor=kLineColor;
        
        [headView addSubview:lineView];
        
        
        
        if (!self.tf) {
            
        
        
        UITextField * tf=[[UITextField alloc] init];
        tf.placeholder=@"  请输入您不想吃的食物名称";
        tf.layer.borderWidth=1;
        tf.layer.borderColor=[UIColor lightGrayColor].CGColor;
        [tf setValue:kPlaceholderColor forKeyPath:@"_placeholderLabel.textColor"];
        [tf setValue:kFont(14) forKeyPath:@"_placeholderLabel.font"];
            
        self.tf=tf;
        [headView addSubview:tf];
        
        [tf mas_makeConstraints:^(MASConstraintMaker *make) {
        
            make.top.mas_equalTo(headView.mas_top).with.offset(60);
            make.centerX.mas_equalTo(headView);
            make.size.mas_equalTo(CGSizeMake(kUI_SCREEN_WIDTH-120, 44));
            
            
            
        }];
        
        
        }
        
        
        if (!self.bt) {
    
        
        UIButton * bt=[UIButton buttonWithType:UIButtonTypeCustom];
        [bt setImage:[UIImage imageNamed:@"search"] forState:0];
        
        [headView addSubview:bt];
        [bt mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.size.mas_equalTo(CGSizeMake(44, 44));
        
            make.top.mas_equalTo(self.tf);
            make.right.mas_equalTo(self.tf);
            
            
        }];
        
            [bt addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
            
            self.bt=bt;
        }
        
    }
    
    
    
    
    
    
    
    
    return headView;

 




}


-(void)searchAction
{
    
    if (self.tf.text.length==0) {
        [SVProgressHUD showImage:nil status:@"请输入食材名"];
   
        return;
    }
    

    [WebAPI webAPISignAddMd5tWithNote:@"user/gettaboo" parm:@{
                                                              
                                                              @"search":self.tf.text
                                                              } completed:^(NSInteger state, AFHTTPRequestOperation *operation, id responseObject) {
       
        
                                                                  if ([kStr(responseObject[@"code"]) isEqualToString:@"1"]) {
                                                                      
                                                                      self.alltabooFoods=[responseObject[@"result"] mutableCopy];
                                                                      
                                                                  }
        
        
    }];



}


@end
